from fastapi import APIRouter, status, Cookie, HTTPException

from typing import Annotated, Optional

from src.weather_service.services.forecast import Forecast_service

from src.weather_service.schemas_forecast import (
    Upload_forecast,
    Update_forecast
)


forecast_router = APIRouter(
    prefix="/region/weather/forecast",
    tags=["forecast"]
)


@forecast_router.post(
    "",
    status_code=status.HTTP_200_OK
    # IN Task 200
)
async def create_forecast(
    payload: Upload_forecast,
    id: Annotated[int | None, Cookie()] = None
):
    return await Forecast_service().create_forecast(
        payload,
        user_id=id
    )


@forecast_router.get(
    "/{weatherForecast}",
    status_code=status.HTTP_200_OK
)
async def get_forecast(
    weatherForecast: Optional[int],
    id: Annotated[int | None, Cookie()] = None
):
    if weatherForecast is None or weatherForecast <= 0:
        raise HTTPException(status_code=400, detail="bad data")
    return await Forecast_service().get_forecast(
        weatherForecast,
        user_id=id
    )


@forecast_router.put(
    "/{weatherForecast}",
    status_code=status.HTTP_200_OK
)
async def edit_forecast(
    weatherForecast: int,
    payload: Update_forecast,
    id: Annotated[int | None, Cookie()] = None
):
    if weatherForecast is None or weatherForecast <= 0:
        raise HTTPException(status_code=400, detail="bad data")

    return await Forecast_service().edit_forecast(
        weatherForecast,
        payload,
        user_id=id
    )


@forecast_router.delete(
    "/{weatherForecast}",
    status_code=status.HTTP_200_OK
)
async def delete_forecast(
    weatherForecast: int,
    id: Annotated[int | None, Cookie()] = None
):
    if weatherForecast is None or weatherForecast <= 0:
        raise HTTPException(status_code=400, detail="bad data")

    return await Forecast_service().delete_forecast(
        weatherForecast,
        user_id=id
    )
